#!/usr/bin/python3.4
# -*- coding=utf-8 -*-

import cgi, sys
from formMockup import FieldMockup
from lang2com import hellos, inputkey
debugme = False

dhrhtml = """Content-type: text/html\n
<TITLE>Languages</TITLE>
<H1>Syntax</H1><HR>"""

langhtml = """
<H3>%s</H3><P><PRE>
%s
</PRE></P><BR>"""

def showHello(form):
    choice = form[inputkey].value
	try:
		print(langhtml % (cgi.escape(choice),
						  cgi.escape(hellos[choice])))
	except KeyError:
		print(langhtml % (cgi.escape(choice),
			              "Sorry -- I don'not Know that language"))

def main():
	if debugme:
		form = {inputkey: FieldMockup(sys.argv[1])}
	else:
		form = cgi.FieldStorage()
	print(dhrhtml)

	if not inputkey in form or form[inputkey].value == 'ALL':
		for lang in hellos.keys():
			mock = {inputkey: FieldMockup(lang)}
			showHello(mock)
	else:
		showHello(form)
	print("<HR>")

if __name__ == '__main__':
	main()
